/*
 * Copyright @2011 Dnbiz. All rights reserved.
 */
package com.dnbiz.api.sdk;

import java.util.List;

import com.dnbiz.api.sdk.util.JsonUtil;
import com.dnbiz.api.sdk.util.MD5;
import com.dnbiz.api.sdk.util.Validator;

/**
 * <pre>
 * API访问URL组装
 * </pre>
 * @author HuangFeng(2011-1-21)
 */
class ApiURL {

	private static final String _api_rest   = "http://" + Api._IP + ":" + Api._PORT +  "/api/rest/?api_key="   + Api._KEY + "&api_secret=" + Api._SECRET;
	private static final String _api_upload = "http://" + Api._IP + ":" + Api._PORT +  "/api/upload/?api_key=" + Api._KEY + "&api_secret=" + Api._SECRET;
	private static final String _api_format = "format=" + Api._FORMAT;
	
	public static final String get(String method)
	{
		return addUrlMethod(_api_rest, method).toString();
	}
	
	/**
	 * <pre>
	 * API访问URL
	 * 当mehtod为空时，<code>throw new NullPointerException("方法不能为空")</code>
	 * 当param或value为空时，<code>throw new NullPointerException("参数不能为空")</code>
	 * </pre>
	 * @author HuangFeng(2011-01-21)
	 * @param method 调用API方法  <code>not null</code>
	 * @param param Api参数名 <code>not null</code>
	 * @param value Api参数值 <code>not null</code>
	 * @return API访问URL
	 */
	public static final String get(String method, String param, String value)
	{
		return get(method, new String[] { param }, new String[] { value });
	}
	
	/**
	 * <pre>
	 * API访问URL
	 * 当mehtod为空时，<code>throw new NullPointerException("方法不能为空")</code>
	 * 当params或values为空时，<code>throw new NullPointerException("参数不能为空")</code>
	 * 当params.length != values.length ，<code>throw new ArrayIndexOutOfBoundsException("参数名和参数值不相等")</code>
	 * </pre>
	 * @author HuangFeng(2011-01-21)
	 * @param method 调用API方法  <code>not null</code>
	 * @param params Api参数名 <code>not null</code>
	 * @param values Api参数值 <code>not null</code>
	 * @return API访问URL
	 */
	public static final String get(String method, String[] params, String[] values)
	{
		if(Validator.isNull(method))
		{
			throw new NullPointerException("方法不能为空");
		}
		
		if(Validator.isNull(params) || Validator.isNull(values))
		{
			throw new NullPointerException("参数不能为空");
		}
		
		if(params.length != values.length)
		{
			throw new ArrayIndexOutOfBoundsException("参数名和参数值不相等");
		}
		
		ApiParameter<String, Object> apiParams = new ApiParameter<String, Object>();
		for(int i = 0; i < params.length; i++)
		{
			apiParams.put(params[i], values[i]);
		}
		
		return get(method, apiParams);
		
	}
	
	/**
	 * <pre>
	 * API访问URL
	 * 当mehtod为空时，<code>throw new NullPointerException("方法不能为空")</code>
	 * 当params为空时，<code>throw new NullPointerException("参数不能为空")</code>
	 * </pre>
	 * @author HuangFeng(2011-01-21)
	 * @param method 调用API方法  <code>not null</code>
	 * @param params Api其他参数 <code>not null</code>
	 * @return API访问URL
	 */
	@SuppressWarnings("unchecked")
	public static final String get(String method, ApiParameter<String, Object> params)
	{
		if(Validator.isNull(method))
		{
			throw new NullPointerException("方法不能为空");
		}
		
		if(Validator.isEmpty(params))
		{
			throw new NullPointerException("参数不能为空");
		}
		
		StringBuilder sb = addUrlMethod(_api_rest, method);
		for(String key : params.keySet())
		{
			if(params.get(key) instanceof List){				
				sb.append(key).append("=").append(JsonUtil.getStringByList((List)params.get(key))).append("&");
			}else{
				sb.append(key).append("=").append(params.get(key)).append("&");
			}
		}
		
		return getApiEncode(sb.toString());
	}
	
	private static final String getApiEncode(String apiurl)
	{
		String   quaryString = apiurl.substring(apiurl.indexOf("?") + 1);
		String[] splitStrs   = quaryString.split("&");
		StringBuilder encodeStrBuf = new StringBuilder();
		for(String s : splitStrs)
		{
			encodeStrBuf.append(s.split("=")[1]);
		}
		return apiurl + "api_encode=" + MD5.encode(encodeStrBuf.toString());
	}
	
	/**
	 * 文件及图片上传ApiUrl
	 * @author HuangFeng(2011-1-26)
	 * @param method 调用API方法，<code>mall365.product.addimg</code>
	 * @return upload ApiUrl
	 */
	public static final String upload(String method)
	{
		return addUrlMethod(_api_upload, method).toString();
	}
	
	/**
	 * APIURL添加操作方法
	 * @author HuangFeng(2011-01-21)
	 * @param method API调用方法,如：<code>mall365.cache.refresh</code>
	 */
	private static StringBuilder addUrlMethod(String apiHttpUrl, String method)
	{
		StringBuilder sb = new StringBuilder(apiHttpUrl);
		sb.append("&t=").append(System.currentTimeMillis()).append("&");
		sb.append("method=").append(method).append("&");
		sb.append(_api_format).append("&");
		return sb;
	}
	
}
